package cn.xpride.framework.utils;

import lombok.experimental.UtilityClass;

import java.io.*;

@UtilityClass
public class FileUtil {

    /**
     * 以UTF-8编码读文件
     *
     * @param fileName
     * @return
     */
    public static String readFileString(String fileName) {
        byte[] data = readFile(fileName);
        return null == data ? null : new String(data);
    }

    /**
     * @param fileName
     * @param data
     */
    public static void writeFileString(String fileName, String data) {
        writeFile(fileName, data.getBytes());
    }

    public static byte[] readFile(String fileName) {//可以保留文件的所有字符
        BufferedInputStream bis = null;
        try {
            File f = new File(fileName);
            if (!f.exists()) {
                throw new FileNotFoundException(fileName);
            }
            bis = new BufferedInputStream(new FileInputStream(f));
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
            int bufSize = 1024;
            byte[] buffer = new byte[bufSize];
            int len = 0;
            while (-1 != (len = bis.read(buffer, 0, bufSize))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static byte[] readDataFromStream(InputStream inputStream) {
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(inputStream);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(inputStream.available());
            int bufSize = 1024;
            byte[] buffer = new byte[bufSize];
            int len = 0;
            while (-1 != (len = bis.read(buffer, 0, bufSize))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    public static void writeFile(String fileName, byte[] bytes) {
        BufferedOutputStream bos = null;
        try {
            File parentFile = new File(fileName).getParentFile();
            while (!parentFile.exists()) {
                boolean r = parentFile.mkdirs();
                if (!r) {
                    throw new RuntimeException("cant't create path!");
                }else {
                    parentFile = parentFile.getParentFile();
                }
            }
            bos = new BufferedOutputStream(new FileOutputStream(fileName));
            bos.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
